using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class InventoriesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class InventoriesDbo : DatabaseObject
        {
            public InventoriesDbo()
            {
                Name = "Inventories";
                GeneratedClassName = "Inventories";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Description", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Description" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Quantity", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Quantity" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Cost", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Cost" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Retail", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Retail" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GP", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GP" });
              
            }
        }
		
		public static List<InventoryRow> Select(Expression<Func<InventoryRow, bool>> expression)
        {
            List<InventoryRow> rows = new List<InventoryRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<InventoryRow>(new InventoriesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<InventoryRow> Select()
		{
			List<InventoryRow> rows = new List<InventoryRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<InventoryRow>(new InventoriesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<InventoryRow> PagedSelect(Expression<Func<InventoryRow, bool>> filterExpression, Expression<Func<InventoryRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<InventoryRow> rows = new List<InventoryRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<InventoryRow>(new InventoriesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<InventoryRow> InventoryList)
        {
            InventoryRow row;

            while (reader.Read())
            {
                row = new InventoryRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					StoreId = Util.CastDbValue<int>(reader["StoreId"]),
					ArticleId = Util.CastDbValue<int>(reader["ArticleId"]),
					Description = Util.CastDbValue<string>(reader["Description"]),
					Quantity = Util.CastDbValue<decimal?>(reader["Quantity"]),
					Cost = Util.CastDbValue<decimal>(reader["Cost"]),
					Retail = Util.CastDbValue<decimal?>(reader["Retail"]),
					GP = Util.CastDbValue<decimal?>(reader["GP"])
                };
                                
                InventoryList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(InventoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<InventoryRow>(new InventoriesDbo(), row);
            }
        }

		public static void Update(InventoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<InventoryRow>(new InventoriesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<InventoryRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<InventoryRow>(new InventoriesDbo(), expression);
            }
        }

	}
}
	
